#!/usr/bin/python

import cgi
import cgitb; cgitb.enable()
import os
import urllib

CONTENTS_DIR="contents"

def page_file_name(page):
    file_name="%s.txt" % urllib.quote_plus(page)
    return os.path.join(CONTENTS_DIR,file_name)

def page_contents(page):
    file_name=page_file_name(page)
    if not os.path.exists(file_name):
        return None
    return file(file_name).read()

def page_header(title):
    print "Content-Type: text/html"
    print
    print "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt'>"
    print "<html xmlns='http://www.w3.org/1999/xhtml'>"
    print "<head>"
    print "<title>%s</title>" % cgi.escape(title)
    print "</head>"
    print "<body>"

def page_footer():
    print "</body>"
    print "</html>"

def view_page(page,params):
    contents=page_contents(page)
    if contents is None:
		contents='page not found'
    #    edit_page(page,params)
    #    return
    
    page_header(page)
    print "<h1>%s</h1>" % cgi.escape(page)
    print "<div class='contents'>"
    print cgi.escape(contents)
    print "</div>"
    page_footer()

def execute(params):
    page=params.getfirst('page','main')
    view_page(page,params)

params=cgi.FieldStorage()
execute(params)
